﻿using NLog;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OrBitDataModule.Common
{
    /// <summary>
    /// 日志类
    /// </summary>
    public static class Logger
    {
        private static NLog.Logger logger = LogManager.GetCurrentClassLogger();
        public static void Info(string message) => logger.Info(message);
        public static void Error(string message) => logger.Error(message);
        public static void Debug(string message) => logger.Debug(message);
        public static void Warn(string message) => logger.Warn(message);
        public static void Trace(string message) => logger.Trace(message);
        public static void Error(Exception ex) => logger.Error(GetExceptionMsg(ex, string.Empty));
        public static void Log(string message, LogLevel logLevel) => logger.Log(logLevel, message);

        /// <summary>
        /// 生成自定义异常消息
        /// </summary>
        /// <param name="ex">异常对象</param>
        /// <param name="backStr">备用异常消息：当ex为null时有效</param>
        /// <returns>异常字符串文本</returns>
        public static string GetExceptionMsg(Exception ex, string backStr)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("****************************异常文本****************************");
            sb.AppendLine("【出现时间】：" + DateTime.Now.ToString());
            if (ex != null)
            {
                sb.AppendLine("【异常类型】：" + ex.GetType().Name);
                sb.AppendLine("【异常信息】：" + ex.Message);
                sb.AppendLine("【堆栈调用】：" + ex.StackTrace);
            }
            else
            {
                sb.AppendLine("【未处理异常】：" + backStr);
            }
            sb.AppendLine("***************************************************************");
            return sb.ToString();
        }

        /// <summary>
        /// 写日志
        /// </summary>
        /// <param name="message"></param>
        /// <param name="color"></param>
        /// <param name="memberName"></param>
        /// <param name="sourceLineNumber"></param>
        public static void WriteLog(RichTextBox richTextBox, string message, bool isError = false, [CallerFilePath] string sourceFilePath = "", [CallerMemberName] string memberName = "", [CallerLineNumber] int sourceLineNumber = 0)
        {
            richTextBox.Invoke(new Action(() =>
            {
                if (richTextBox.GetLineFromCharIndex(richTextBox.Text.Length) > 1000)
                {
                    richTextBox.Text = "";
                }
                richTextBox.SelectionStart = richTextBox.Text.Length;
                if (isError)
                {
                    richTextBox.SelectionStart = richTextBox.Text.Length;
                    richTextBox.SelectionColor = Color.Red;
                    message = $"{DateTime.Now.ToString("HH:mm:ss")} : 代码文件:{Path.GetFileName(sourceFilePath)} 方法名:{memberName} 行号:{sourceLineNumber}  消息:{message}";
                    Error(message);
                    richTextBox.AppendText(message + Environment.NewLine);
                }
                else
                {
                    Info($"{DateTime.Now.ToString("HH:mm:ss")} : 消息:{message}");
                    richTextBox.AppendText($"{DateTime.Now.ToString("HH:mm:ss")} : 消息:{message}\r\n");
                }
                richTextBox.ScrollToCaret();
            }));
        }
    }
}
